package net.dgg.rz.common.base.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.rz.common.base.entity.BaseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @author nature
 * 2018-04-17
 */
@Service
public class BaseService {


    public <T extends BaseEntity> T saveAssem(T entity, Long userId, String userName) {
        Date date = new Date();
        entity.setId(DggKeyWorker.nextId());
        entity.setCreaterId(userId);
        entity.setCreateTime(date);
        entity.setCreaterName(userName);
        entity.setUpdateTime(date);
        return entity;
    }


    public <T extends BaseEntity> T updateAssem(T entity, Long userId, String userName) {
        entity.setUpdaterId(userId);
        entity.setUpdaterName(userName);
        entity.setUpdateTime(new Date());
        return entity;
    }


    public <T extends BaseEntity> T saveOpt(T entity, UserEntity user) {
        Date date = new Date();
        entity.setCreaterId(user.getId());
        entity.setCreateTime(date);
        entity.setCreaterName(user.getRealName() + user.getLoginName());
        entity.setCreaterOrgId(user.getOrgId());
        entity.setUpdateTime(date);
        entity.setUpdaterId(user.getId());
        entity.setUpdaterName(user.getRealName() + user.getLoginName());
        entity.setUpdaterOrgId(user.getOrgId());
        return entity;
    }


    public <T extends BaseEntity> T updateOpt(T entity, UserEntity user) {
        entity.setUpdateTime(new Date());
        entity.setUpdaterId(user.getId());
        entity.setUpdaterName(user.getRealName() + user.getLoginName());
        entity.setUpdaterOrgId(user.getOrgId());
        return entity;
    }

}
